<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" href="__PUBLIC__/css/jquery-ui-1.8.18.custom.css">
<script src="__PUBLIC__/js/jquery-1.7.1.min.js"></script>
<script src="__PUBLIC__/js/jquery-ui-1.8.18.custom.min.js"></script>
<script>
$(document).ready(function(){
	var edit = $("span[name='edit']");
	edit.click(function(){
		var id = $(this).parents("tr").children('td:first').html();
		var url = "__URL__/getRoleMenus";
		var data = "id="+id;
		$.post(url,data,function(res){
			$(":checkbox[name='menus[]']").attr('checked',false);
			$('#id').val(res['id']);
			$('#name').val(res['name']);
			$('#sort').val(res['sort']);
			$('#status').find("option").each(function(){
				if($(this).val()==res['status']){
					$(this).attr('selected','selected');
				}
			});
			var menus = res['menuArray'];
			$(":checkbox[name='menus[]']").each(function(){
				var v = $(this).val();
				t = $.inArray(v,menus);
				if(t!=-1){
					$(this).attr('checked','true');
				}
			});
		},'json');
	});
	
	var del = $("span[name='del']");
	del.click(function(){
		var id = $(this).parents("tr").children('td:first').html();
		var tips = "确定要删除ID为"+id+"的角色吗?";
		var sure = confirm(tips);
		if(sure){
			var url = "__URL__/delete";
			var data = "id="+id;
			$.post(url,data,function(res){
				if(res==1){
					alert("删除成功");
					location.reload();
				}else{
					alert("删除失败");
				}
			});
		}
	});
	
	$("reset").click(function(){
		$(":checkbox[name='menus[]']").attr('checked',false);
		$('#id').val(0);
	});
	
	$("#save").click(function(){
		//如果有id则更新，否则新增
		var url = "__URL__/insert";
		var name = $("#name").val();
		var status = parseInt($('#status').find("option:selected").val());
		var sort = $("#sort").val();
		var menus = '';
		$(":checkbox[name='menus[]'][checked]").each(function(){
			menus += $(this).val()+ "," ;
		});
		var data = "name="+name+"&status="+status+"&sort="+sort+"&menus="+menus;
		var id = parseInt($("#id").val());
		if(id!=0){
			url= "__URL__/update";
			data += "&id="+id;
		}
		
		if(name != '' && sort != ''){
			$.post(url,data,function(res){
				if(res==1){
					alert("保存成功");
					location.reload();
				}else{
					alert("保存失败");
				}
			});
		}
	});
	$(":checkbox[name='menus[]']").change(function(){
	});
});
</script>
<style>
span{cursor:pointer;}
li{list-style: none;display:inline;width:100px;font-weight:normal; font-size:12px;}
</style>
</head>
<body>


<table cellspacing="1"  style="background:black;width:30%;float:left;">
	<tr style="background:white;"><th colspan="5">角色列表</th></tr>
	<tr style="background:white;"><th>ID</th><th>角色名</th><th>排序</th><th>状态</th><th>操作</th></tr>
	<volist name="roles" id="role">
		<tr style="background:white;"><td>{$role.id}</td><td>{$role.name}</td><td>{$role.sort}</td><td><if condition="$role.status eq 1">正常<else/>禁用</if></td><td><span name="edit">编辑</span>｜<span name="del">删除</span></td></tr>
	</volist>
</table>
<table cellspacing="1" style="background:black;width:69%;float:left;margin-left:1%;">
	<form method="post">
	<input type="hidden" name="id" id="id" value="0"/>
	<tr style="background:white;"><td>name:</td><td><input name="name" id="name" type="text"/></td></tr>
	<tr style="background:white;"><td>status:</td>
	<td>
		<select name="status" id="status">
			<option value="1">正常</option>
			<option value="0">禁用</option>
		</select>
	</td></tr>
	<tr style="background:white;"><td>sort:</td><td><input name="sort" id="sort" type="text" value="0"/></td></tr>
	<tr style="background:white;"><th colspan="2">权限管理</th></tr>
	<volist name="menu" id="vo">
		<tr style="background:white;" >
		<td width="100" colspan="2" style="font-weight:bold;"><input type="checkbox" name="menus[]" value="{$vo.id}"/>{$vo.name}
			<if condition="$vo.sub">
				<volist name="vo['sub']" id="s">
						<ul><input type="checkbox" name="menus[]" value="{$s.id}"/>{$s.name}
							<if condition="$s.sub">
								<volist name="s['sub']" id="l">
									<li><input type="checkbox" name="menus[]" value="{$l.id}"/>{$l.name}</li>
								</volist>
							</if>
						</ul>
				</volist>
			</if>
			</td>
		</tr>
	</volist>
	<tr style="background:white;"><td colspan="2" align="center"><input name="submit" type="button" id="save" value="保存"/>
		<input name="reset" id="reset" type="reset" value="重置"/>
	</td></tr>
	</form>
</table>
</body>
</html>